CREATE OR REPLACE PROCEDURE tssxsp_to_fbchs(p_phid IN NUMBER) 
AS
u_sourceid_m BIGINT;
BEGIN

    SELECT u_fbxmchsbh into u_sourceid_m from p_form_tssxsp_m where phid = p_phid;

        -- 更新明细1
    FOR rec IN (
        SELECT *
        FROM p_form_tssxsp_d2 WHERE pphid = p_phid
    ) LOOP

        IF rec.u_sourceid IS NULL OR rec.u_sourceid = '' THEN
            INSERT INTO p_form_fbch_d2
            (
                phid,
                pphid,
                rel_key1,
                u_fbxmmc
            ) VALUES(
                fbch_insert_phid_d2.nextval,
                u_sourceid_m,
                rec.rel_key1,
                rec.u_fbxmmc
            );
        ELSE
            UPDATE p_form_fbch_d2 
            SET u_fbxmmc = rec.u_fbxmmc WHERE phid = rec.u_sourceid;
        END IF;
    END LOOP;

        -- 更新明细2
    FOR rec2 IN (
        SELECT *
        FROM p_form_tssxsp_d3 WHERE pphid = p_phid
    ) LOOP
        IF rec2.u_sourceid IS NOT NULL AND rec2.u_sourceid != '' THEN
            UPDATE p_form_fbch_d3 
                SET
                    s_tree_no      = rec2.s_tree_no,
                    s_tree_name    = rec2.s_tree_name,
                    u_dw           = rec2.u_dw,
                    u_gcl          = rec2.u_gcl,
                    u_dj           = rec2.u_dj,
                    u_hj           = rec2.u_hj,
                    u_nfbje        = rec2.u_nfb1,
                    u_lr           = rec2.u_lr1,
                    u_cl           = rec2.u_cl1,
                    u_jx           = rec2.u_jx1,
                    u_qtfy         = rec2.u_qtfy,
                    u_sj           = rec2.u_sj1,
                    u_fyxj         = rec2.u_fyxj1,
                    u_clje         = rec2.u_cl2,
                    u_qtfyje       = rec2.u_qtfy2,
                    u_sjje         = rec2.u_sj2,
                    u_fyxjje       = rec2.u_fyxj2,
                    u_dydcbhtje    = rec2.u_dydcbhtje,
                    u_lrje         = rec2.u_lr2,
                    u_nfb          = rec2.u_nfb2,
                    u_bz           = rec2.u_bz
                WHERE phid = rec2.u_sourceid;
        ELSE 
            -- 执行插入
            INSERT INTO p_form_fbch_d3 
            (
                phid,
                pphid,
                rel_key1,
                s_tree_no,
                s_tree_name,
                u_dw,
                u_gcl,
                u_dj,
                u_hj,
                u_nfbje,
                u_lr,
                u_cl,
                u_jx,
                u_qtfy,
                u_sj,
                u_fyxj,
                u_clje,
                u_qtfyje,
                u_sjje,
                u_fyxjje,
                u_dydcbhtje,
                u_lrje,
                u_nfb,
                u_bz,
                s_tree_pid,
                s_tree_id
            ) VALUES(
                fbch_insert_phid_d3.nextval,
                u_sourceid_m,
                rec2.rel_key1,
                rec2.s_tree_no,
                rec2.s_tree_name,
                rec2.u_dw,
                rec2.u_gcl,
                rec2.u_dj,
                rec2.u_hj,
                rec2.u_nfb1,
                rec2.u_lr1,
                rec2.u_cl1,
                rec2.u_jx1,
                rec2.u_qtfy,
                rec2.u_sj1,
                rec2.u_fyxj1,
                rec2.u_cl2,
                rec2.u_qtfy2,
                rec2.u_sj2,
                rec2.u_fyxj2,
                rec2.u_dydcbhtje,
                rec2.u_lr2,
                rec2.u_nfb2,
                rec2.u_bz,
                rec2.s_tree_pid,
                rec2.s_tree_id
            );
        END IF;
    END LOOP;

    COMMIT;
END;
